﻿package utils{
	import flash.geom.Point;

	/**
	 *
	 * 字符串相关操作。
	 *
	 */
	public class StringUtil{

		/**
		 * 忽略大小字母比较字符是否相等;
		 * @param char1
		 * @param char2
		 * @return
		 *
		 */
		public static function equalsIgnoreCase(char1:String,char2:String):Boolean{
			return char1.toLowerCase() == char2.toLowerCase();
		}

		/**
		 * 比较字符是否相等;
		 * @param char1
		 * @param char2
		 * @return
		 *
		 */
		public static function equals(char1:String,char2:String):Boolean{
			return char1 == char2;
		}

		/**
		 * 是否为Email地址;
		 * @param char
		 * @return
		 *
		 */
		public static function isEmail(char:String):Boolean{
			if(char == null){
				return false;
			}
			char = trim(char);
			var pattern:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;
			var result:Object = pattern.exec(char);
            if(result == null) {
                return false;
            }
            return true;
		}

		/**
		 * 是否是数值字符串;
		 * @param char
		 * @return
		 *
		 */
		public static function isNumber(char:String):Boolean{
			if(char == null){
				return false;
			}
			return !isNaN(Number(char));
		}

		/**
		 * 是否为Double型数据;
		 * @param char
		 * @return
		 *
		 */
		public static function isDouble(char:String):Boolean{
			char = trim(char);
			var pattern:RegExp = /^[-\+]?\d+(\.\d+)?$/;
			var result:Object = pattern.exec(char);
            if(result == null) {
                return false;
            }
            return true;
		}
		/**
		 * Integer;
		 * @param char
		 * @return
		 *
		 */
		public static function isInteger(char:String):Boolean{
			if(char == null){
				return false;
			}
			char = trim(char);
			var pattern:RegExp = /^[-\+]?\d+$/;
			var result:Object = pattern.exec(char);
            if(result == null) {
                return false;
            }
            return true;
		}

		/**
		 * English;
		 * @param char
		 * @return
		 *
		 */
		public static function isEnglish(char:String):Boolean{
			if(char == null){
				return false;
			}
			char = trim(char);
			var pattern:RegExp = /^[A-Za-z]+$/;
			var result:Object = pattern.exec(char);
            if(result == null) {
                return false;
            }
            return true;
		}
		/**
		 * 中文
		 * @param char
		 * @return
		 *
		 */
		public static function isChinese(char:String):Boolean{
			if(char == null){
				return false;
			}
			char = trim(char);
			var pattern:RegExp = /^[\u0391-\uFFE5]+$/;
			var result:Object = pattern.exec(char);
            if(result == null) {
                return false;
            }
            return true;
		}

		/**
		 * 双字节
		 * @param char
		 * @return
		 *
		 */
		public static function isDoubleChar(char:String):Boolean{
			if(char == null){
				return false;
			}
			char = trim(char);
			var pattern:RegExp = /^[^\x00-\xff]+$/;
			var result:Object = pattern.exec(char);
            if(result == null) {
                return false;
            }
            return true;
		}


		/**
		 * 含有中文字符
		 * @param char
		 * @return
		 *
		 */
		public static function hasChineseChar(char:String):Boolean{
			if(char == null){
				return false;
			}
			char = trim(char);
			var pattern:RegExp = /[^\x00-\xff]/;
			var result:Object = pattern.exec(char);
            if(result == null) {
                return false;
            }
            return true;
		}

		/**
		 * 注册字符;
		 * @param char
		 * @param len
		 * @return
		 *
		 */
		public static function hasAccountChar(char:String,len:uint=15):Boolean{
			if(char == null){
				return false;
			}
			if(len < 10){
				len = 15;
			}
			char = trim(char);
			var pattern:RegExp = new RegExp("^[a-zA-Z0-9][a-zA-Z0-9_-]{0,"+len+"}$", "");
			var result:Object = pattern.exec(char);
            if(result == null) {
                return false;
            }
            return true;
		}

		/**
		 * URL地址;
		 * @param char
		 * @return
		 *
		 */
		public static function isURL(char:String):Boolean{
			if(char == null){
				return false;
			}
			char = trim(char).toLowerCase();
			var pattern:RegExp = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
			var result:Object = pattern.exec(char);
            if(result == null) {
                return false;
            }
            return true;
		}


		/**
		 * 是否为空白;
		 * @param char
		 * @return
		 *
		 */
		public static function isWhitespace(char:String):Boolean{
			switch (char){
	            case " ":
	            case "\t":
	            case "\r":
	            case "\n":
	            case "\f":
	                return true;
				default:
	                return false;
	        }
		}


		/**
		 * 去左右空格;
		 * @param char
		 * @return
		 *
		 */
		public static function trim(char:String):String{
			if(char == null){
				return null;
			}
			return rtrim(ltrim(char));
		}


		/**
		 * 去左空格;
		 * @param char
		 * @return
		 *
		 */
		public static function ltrim(char:String):String{
			if(char == null){
				return null;
			}
			var pattern:RegExp = /^\s*/;
			return char.replace(pattern,"");
		}


		/**
		 * 去右空格;
		 * @param char
		 * @return
		 *
		 */
		public static function rtrim(char:String):String{
			if(char == null){
				return null;
			}
			var pattern:RegExp = /\s*$/;
			return char.replace(pattern,"");
		}


		/**
		 * 是否为前缀字符串;
		 * @param char
		 * @param prefix
		 * @return
		 *
		 */
		public static function beginsWith(char:String, prefix:String):Boolean{
			return (prefix == char.substring(0, prefix.length));
		}


		/**
		 * 是否为后缀字符串;
		 * @param char
		 * @param suffix
		 * @return
		 *
		 */
		public static function endsWith(char:String, suffix:String):Boolean{
			return (suffix == char.substring(char.length - suffix.length));
		}


		/**
		 * 去除指定字符串;
		 * @param char
		 * @param remove
		 * @return
		 *
		 */
		public static function remove(char:String,remove:String):String{
			return replace(char,remove,"");
		}


		/**
		 * 字符串替换;
		 * @param char
		 * @param replace
		 * @param replaceWith
		 * @return
		 *
		 */
		public static function replace(char:String, replace:String, replaceWith:String):String{
			return char.split(replace).join(replaceWith);
		}


		/**
		 * utf16转utf8编码;
		 * @param char
		 * @return
		 *
		 */
		public static function utf16to8(char:String):String{
			var out:Array = new Array();
			var len:uint = char.length;
			for(var i:uint=0;i<len;i++){
				var c:int = char.charCodeAt(i);
				if(c >= 0x0001 && c <= 0x007F){
					out[i] = char.charAt(i);
				} else if (c > 0x07FF) {
					out[i] = String.fromCharCode(0xE0 | ((c >> 12) & 0x0F),
												 0x80 | ((c >>  6) & 0x3F),
												 0x80 | ((c >>  0) & 0x3F));
				} else {
					out[i] = String.fromCharCode(0xC0 | ((c >>  6) & 0x1F),
												 0x80 | ((c >>  0) & 0x3F));
				}
			}
			return out.join('');
		}


		/**
		 * utf8转utf16编码;
		 * @param char
		 * @return
		 *
		 */
		public static function utf8to16(char:String):String{
			var out:Array = new Array();
			var len:uint = char.length;
			var i:uint = 0;
			var char2:int,char3:int;
			while(i<len){
				var c:int = char.charCodeAt(i++);
				switch(c >> 4){
					case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
						// 0xxxxxxx
						out[out.length] = char.charAt(i-1);
						break;
					case 12: case 13:
						// 110x xxxx   10xx xxxx
						char2 = char.charCodeAt(i++);
						out[out.length] = String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
						break;
					case 14:
						// 1110 xxxx  10xx xxxx  10xx xxxx
						char2 = char.charCodeAt(i++);
						char3 = char.charCodeAt(i++);
						out[out.length] = String.fromCharCode(((c & 0x0F) << 12) |
							((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
						break;
				}
			}
			return out.join('');
		}
		public static function stringToPoint(str:String):Point{
		    var ps:Array=str.split(",");
			return new Point(ps[0],ps[1]);
		}
		public static function stringToTime(str:String):Number{
			
            return int(str);
		}
		public static function timeFormat(time:Number):String{
			var d:Date=new Date(0);
			var e:Date=new Date(time);
			var y:int=e.fullYearUTC-d.fullYear;
			var m:int=e.month-d.month;
			var da:int=e.date-d.date;
			
			var fTime:String=intFormat(e.hours-d.hours)+":"+intFormat(e.minutes-d.minutes)+":"+intFormat(e.seconds-d.seconds);
			if(y>0){
				return y+"年"+m+"月"+da+"日"+fTime;
			     
			}
			if(m>0){
				return m+"月"+da+"日"+fTime;
			}
			if(da>0){
				return da+"日"+fTime;
			}
			
			return fTime;
		}
//		/**
//		 * 时间差-毫秒==》 x天x小时x分x秒
//		 * @param time 时间差
//		 * @param tf 文本框
//		 * @return 
//		 * 
//		 */
		public static function timeFormat1(time:Number):String{
			var asl:Number=Math.floor(time/1000);
			var d:Number=Math.floor((asl/60/60)/24);
			var h:Number=Math.floor((asl/60/60)%24);
			var m:Number=Math.floor((asl/60)%60);
			var s:Number=Math.floor(asl%60);
			return toChineseTime(d,h,m,s);
		}
		private static function intFormat(v:Number):String{
		    if(v<10){
			    return "0"+v;
			}
			return String(v)
		}
		private static function toChineseTime(d:Number,h:Number,m:Number,s:Number):String
		{
			if(d>0){
				return d+"天"+(h==0 ? "" : h+"小时");
			}else if(h>0){
				return h+"小时"+(m==0 ? "" : m+"分");
			}else if(m>0){
				return m+"分"+(s==0 ? "" : s+"秒");
			}else if(s>0){
				return (s==0 ? "" : s+"秒");
			}
			return "";
		}
	}
}